import tools from '@/utils/tools'
import fetchService from '@/service/loginService'

let readScoreCache = []
let newsTime = 0
let videoTime = 0

export default {
  add (data) {
    if(typeof data !== 'object') {
      return
    }
    readScoreCache.push(data)
    window.myvue.$localStorage.set('readScoreCache', readScoreCache)
  },
  remove (key) {
    let list = readScoreCache
    readScoreCache.splice(list.findIndex(item => {
      return item.key === key
    }), 1)
    window.myvue.$localStorage.set('readScoreCache', list)
  },
  initList () {
    if (!window.myvue.$localStorage.get('readScoreCache')) {
      window.myvue.$localStorage.set('readScoreCache', [])
    } else {
      readScoreCache = window.myvue.$localStorage.get('readScoreCache')
    }
  },
  initTime () {
    newsTime = window.myvue.$localStorage.get('newsTime') || 0
    videoTime = window.myvue.$localStorage.get('videoTime') || 0
  },
  init () {
    this.initList()
    this.initTime()
    // 定时上传未成功的数据
    setInterval(() => {
      readScoreCache.map(v => {
        this.getScore(v)
      })
    }, 3000)
    setInterval(() => {
      // 在打开第三方页面时添加
      if (sessionStorage.getItem('nativeWindowOpenTime') !== null) {
        this.addTime(1, () => {
          this.pushScore('readTrue')
        })
      }
    }, 1000)
  },
  addTime (type, callback) {
    // 阅读新闻
    if (type === 1) {
      newsTime++
      // console.log('阅读新闻：' + newsTime)
      window.myvue.$localStorage.set('newsTime', newsTime)
      if (newsTime >= 180) {
        callback()
        newsTime = 0
        window.myvue.$localStorage.set('newsTime', 0)
      }
    } else {
      videoTime++
      // console.log('浏览音视频：' + videoTime)
      window.myvue.$localStorage.set('videoTime', videoTime)
      if (videoTime >= 180) {
        callback()
        videoTime = 0
        window.myvue.$localStorage.set('videoTime', 0)
      }
    }
  },
  getScore (data) {
    let methodName = data.methodName
    let key = data.key
    let params = data.params
    let methods = {
      read: 'getcontentRead',   // 有效阅读
      readTrue: 'getstudyTime',   // 阅读时长
      watchTrue: 'getwatchVideoTime',   // 视听时长
      watch: 'getwatchVideo'   // 有效视听
    }
    // 如果参数不全则需要删除记录
    if (!params.userId || !params.businessId) {
      return this.remove(key)
    }
    window.myvue.fetchService[methodName](params).then(res => {
      if (res.code === 0) {
        this.remove(key)
      }
    })
  },
  pushScore (type) {
    let methods = {
      read: 'getcontentRead',   // 有效阅读
      readTrue: 'getstudyTime',   // 阅读时长
      watchTrue: 'getwatchVideoTime',   // 视听时长
      watch: 'getwatchVideo'   // 有效视听
    }
    let params = {
      businessId: 'theThirdPage',
      userId: tools.getUserInfo().id
    }
    let key = Date.now()
    if (!params.userId || !params.businessId) {
      return false
    }
    tools.readScore.add(
      {
        key,
        status: 1,
        methodName: methods[type], // 接口名称
        params // 请求参数
      }
    )
    fetchService[methods[type]](params).then(res => {
      if (res.code === 0) {
        tools.readScore.remove(key)
      }
    })
  }
}
